package com.cheejee.goodscenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.carry.common.tool.StringTool;
import com.cheejee.goodscenter.dao.BrandPropertiesTemplateMapper;
import com.cheejee.goodscenter.dao.GoodsBrandPropertiesMapper;
import com.cheejee.goodscenter.module.entity.BrandPropertiesTemplate;
import com.cheejee.goodscenter.module.entity.CategoryPropertiesTemplate;
import com.cheejee.goodscenter.service.BrandPropertiesTemplateService;
import com.cheejee.goodscenter.service.exception.CheckException;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.service.exception.DuplicateException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.cheejee.goodscenter.validated.UpdateGroup;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @author CARRY
 * @Date 2020年10月23日下午12:00:13
 */
@Service
public class BrandPropertiesTemplateServiceImpl implements BrandPropertiesTemplateService {

	@Resource
	private BrandPropertiesTemplateMapper bptMapper;
	
	@Resource
	private GoodsBrandPropertiesMapper gbpMapper;

	@Override
	public Page<BrandPropertiesTemplate> queryBrandPropertiesTemplate(Integer brandPropertiesGroupId, Integer brandId,
			String propertiesName, Boolean required, Byte type, String initial, int pageNum, int pageSize) {
		
		return PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> bptMapper.queryBySelective(brandId, brandPropertiesGroupId, propertiesName, required, type, initial));
	}

	@Override
	public BrandPropertiesTemplate queryBrandPropertiesTemplateById(Integer id) {

		return bptMapper.selectByPrimaryKey(id);
	}

	@Override
	@Check(message = "新增的品牌属性模板不能为空", group = InsertGroup.class)
	public int addBrandPropertiesTemplate(BrandPropertiesTemplate propertiesTemplate) {

		if (!CollectionUtil.isEmpty(bptMapper.queryBySelective(null, null, propertiesTemplate.getPropertiesName(), null, null, null))) {
			throw new DuplicateException("新增的品牌属性模板名称重复");
		}

		propertiesTemplate.setInitial(StringTool.getInitial(propertiesTemplate.getPropertiesName()));

		return bptMapper.insertSelective(propertiesTemplate);
	}

	@Override
	@Check(message = "修改的品牌属性模板不能为空", group = UpdateGroup.class)
	public int changeBrandPropertiesTemplate(BrandPropertiesTemplate propertiesTemplate) {

		checkExist(propertiesTemplate.getId());
		checkNameChange(propertiesTemplate);
		List<BrandPropertiesTemplate> lb = bptMapper.queryBySelective(null, null, propertiesTemplate.getPropertiesName(), null, null, null);
		
		if (!CollectionUtil.isEmpty(lb) && lb.get(0)
				.getId() != propertiesTemplate.getId()) {
			throw new DuplicateException("修改的品牌属性模板名称重复");
		}

		propertiesTemplate.setInitial(StringTool.getInitial(propertiesTemplate.getPropertiesName()));

		return bptMapper.updateByPrimaryKeySelective(propertiesTemplate);
	}

	@Override
	public int deleteBrandPropertiesTemplate(Integer... id) {

		return bptMapper.deleteById(id);
	}
	
	private void checkNameChange(BrandPropertiesTemplate pt) {
		if (!CollectionUtil.isEmpty(gbpMapper.selectBySelective(null, pt.getId(), null))) {
			String oldName = bptMapper.selectByPrimaryKey(pt.getId()).getPropertiesName();
			String newName = pt.getPropertiesName();
			if(!oldName.equals(newName)) {
				throw CheckException.builder().checkMessage("模板已被使用，不能修改属性名称").build();
			}
		}
	}
	
	private void checkExist(Integer id) {
		if(bptMapper.selectByPrimaryKey(id) == null) {
			throw new DataNotExistException("品牌属性模板不存在");
		}
	}

}
